import pandas as pd

# from utils import get_image_base64, get_model_config, timeit

encoded_df = pd.read_excel("resource/encoded_img.xlsx")


def get_based_img2(image_name, model_name):
    # TODO 错误处理
    try:
        based_img = encoded_df[encoded_df["img_name"] == (
            image_name + ".jpg")]["encoded_img"].tolist()[0]
        # print("image_name:", image_name, "base64_img:", based_img[:5])
        if not model_name.startswith("glm"):
            based_img = f"data:image/jpeg;base64,{based_img}"
        return based_img
    except:
        print("Error:image_name:", image_name)

        return


import base64


def get_based_img3(img_path, model_name):
    with open(img_path, "rb") as img_file:
        based_img = base64.b64encode(img_file.read()).decode("utf-8")
    if not model_name.startswith("glm"):
        based_img = f"data:image/jpeg;base64,{based_img}"
    return based_img


def get_messages(img_name):

    messages = [
        {
            "role": "system",
            "content": "你是一个图像识别专家, 请根据你对图片进行识别",
        },
        {
            "role":
            "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url":
                        get_based_img3("image/" + img_name,
                                       "moonshot-v1-vision-preview")
                    },
                    # "image_url": {"url": get_based_img2("image/" + img_name)},
                    # "image_url": {
                    #     "url": "https://img1.baidu.com/it/u=1369931113,3388870256&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1703696400&t=f3028c7a1dca43a080aeb8239f09cc2f"
                    # },
                },
                {
                    "type":
                    "text",
                    "text":
                    f"""请识别这张图片是自拍还是他拍;是站姿还是坐姿或是卧姿;
                        只需要输出结果即可. 如[自拍,站姿]或[他拍、坐姿]
                        请对以上图片进行识别
                        """,
                },
            ],
        },
    ]
    return messages


df = pd.read_excel("resource/encoded_img.xlsx").iloc[5:10, :]
# def get_based_img2(image_name):
#     encoded_df = pd.read_excel("resource/encoded_img.xlsx")
#     # TODO 错误处理
#     try:
#         based_img = encoded_df[encoded_df["img_name"] == img_name][
#             "encoded_img"
#         ].tolist()[0]
#         print("image_name:", image_name, "base64_img:", based_img[:5])
#         return based_img[24:]
#     except:
#         print("Error:image_name:", image_name)
for idx, row in df.iterrows():
    # print(row["img_name"], row["encoded_img"])
    # print(row["img_name"])
    img_name = row["img_name"]
    print(img_name)
    encoded_img = row["encoded_img"]
    messages = get_messages(img_name)

    for model in keep_model:
        start_time = time.time()
        #     for chunk in chat_with_model(model, messages, stream):
        #         end_time = time.time()
        #         print(f"Time taken for {model}:", round(end_time - start_time, 2), "seconds")
        #         print(chunk)
        chat_with_model(model_name=model, messages=messages, stream=stream)
        end_time = time.time()
        print(f"Time taken for {model}:", end_time - start_time, "seconds")
